package com.telecom.restaurant.controllor.user;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.UserThreadLocal;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.domin.PaginationResult;
import com.telecom.restaurant.domin.vo.OrderConfirmVO;
import com.telecom.restaurant.domin.vo.OrderVO;
import com.telecom.restaurant.enums.OrderStatusEnum;
import com.telecom.restaurant.exception.BizException;
import com.telecom.restaurant.model.param.OrderSearchParam;
import com.telecom.restaurant.service.OrderService;
import com.telecom.restaurant.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by qiqi on 2019/12/22.
 */
@Controller
@RequestMapping("/user/order")
public class UserOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping("/preOrder")
    @ResponseBody
    public DataResult preOrder() {
        // 检查下单时间 TODO for test 临时取消检查
//        if (orderService.returnDayIfCanOrder() < 0) {
//            return DataResultConstant.WRONG_ORDER_TIME;
//        }
        Long userId = UserThreadLocal.userId.get();
        try {
            List<OrderConfirmVO> result = orderService.preOrder(userId);
            pictureService.renderOrderConfirmList(result);
            DataResult dataResult = DataResultConstant.buildSuccessResult(result);
            return dataResult;
        } catch (BizException bizE) {
            DataResult errorResult = bizE.getDataResult();
            return errorResult;
        }
    }

    @RequestMapping("/confirm")
    @ResponseBody
    public DataResult confirmOrder(Boolean userCoupon) {

        if (userCoupon == null) {
            userCoupon = true;
        }

        // 检查下单时间 TODO for test 临时取消检查
//        if (orderService.returnDayIfCanOrder() < 0) {
//            return DataResultConstant.WRONG_ORDER_TIME;
//        }
        Long userId = UserThreadLocal.userId.get();
        try {
            List<OrderVO> result = orderService.confirmOrder(userId, userCoupon);
            DataResult dataResult = DataResultConstant.buildSuccessResult(result);
            return dataResult;
        } catch (BizException bizE) {
            DataResult errorResult = bizE.getDataResult();
            return errorResult;
        }
    }

    @RequestMapping("/getByOrderId")
    @ResponseBody
    public DataResult getByOrderId(Long orderId) {

        if (orderId == null) {
            return DataResultConstant.WRONG_PARAM;
        }

        String mobile = UserThreadLocal.mobile.get();
        OrderVO vo = orderService.getOrderVOById(orderId);
        if (!mobile.equals(vo)) {
            vo = null;
        }
        DataResult dataResult = DataResultConstant.buildSuccessResult(vo);
        return dataResult;
    }


    @RequestMapping("/list")
    @ResponseBody
    public DataResult orderList(Integer pageSize, Integer pageNo) {
        OrderSearchParam param = new OrderSearchParam();
        param.setPageSize(pageSize);
        param.setPageNo(pageNo);
        param.setWithFoodInfo(true);

        Long userId = UserThreadLocal.userId.get();
        param.setCustomerId(userId);
        param.setUseDesc(true);

        PaginationResult result = orderService.searchOrderForShow(param);

        DataResult dataResult = DataResultConstant.buildSuccessResult(result);
        return dataResult;
    }


    @RequestMapping("/cancel")
    @ResponseBody
    public DataResult cancelOrder(Long orderId) {
        if (orderId == null) {
            return DataResultConstant.WRONG_PARAM;
        }

        Long userId = UserThreadLocal.userId.get();
        boolean result = false;

        try {
            result = orderService.cancelOrder(orderId, OrderStatusEnum.CANCEL_MANUALLY);
        } catch (BizException bizE) {
            DataResult errorResult = bizE.getDataResult();
            return errorResult;
        }

        DataResult dataResult = DataResultConstant.buildSuccessResult(result);
        return dataResult;
    }

}
